﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;


/// <summary>
/// Summary description for ServiceDao
/// </summary>
public class ServiceDao
{
    #region "variable"
    private ServiceDto dto;
    private string _error;
    private MyConnection _myConn;     
    #endregion

    #region "properties"
    public string Error
    {
        get { return _error; }
        set { _error = value; }
    }
    public ServiceDto Dto
    {
        get { return dto; }
        set { dto = value; }
    }
    #endregion

	public ServiceDao()
	{
        _myConn = new MyConnection();
        Dto = new ServiceDto();
        Error = "";
	}

    public void InsertServiceType(string _serviceName, bool _status)
    {
        string spName = "sp_Insert_ServiceType";
        SqlParameter[] parameter = { new SqlParameter("@_serviceName", SqlDbType.NVarChar,20),
                                        new SqlParameter("@_status",SqlDbType.Bit)};
        parameter[0].Value = _serviceName;
        parameter[1].Value = _status;
        _myConn.ExecuteNonQuery(spName, parameter);
    }
    public List<ServiceDto> SelectOtherServiceType()
    {
        string spName = "sp_Select_OtherServicesType";
        DataTable table = _myConn.GetDataTable(spName, 1);
        return this.MakeListServices(table);
    }
    public void UpdateServiceDetailStatus(int id_SD, bool _status)
    {
        string spName = "sp_Update_ServiceDetailStatus";
        SqlParameter[] parameter = { new SqlParameter("@id_SD", SqlDbType.Int),
                                        new SqlParameter("@_status",SqlDbType.Bit)};
        parameter[0].Value = id_SD;
        parameter[1].Value = _status;
        _myConn.ExecuteNonQuery(spName, parameter);
    }
    public void UpdateServiceTypeStatus(int id_ST, bool _status)
    {
        string spName = "sp_Update_ServiceTypeStatus";
        SqlParameter[] parameter = { new SqlParameter("@id_ST", SqlDbType.Int),
                                        new SqlParameter("@_status",SqlDbType.Bit)};
        parameter[0].Value = id_ST;
        parameter[1].Value = _status;
        _myConn.ExecuteNonQuery(spName, parameter);
    }
    public void UpdateServiceDetail(ServiceDetail _serviceDetail)
    {
        string spName = "sp_Update_ServiceDetail";
        SqlParameter[] parameter = { new SqlParameter("@id_SD", SqlDbType.Int),
                                     new SqlParameter("@_packageName",SqlDbType.NVarChar,20),
                                     new SqlParameter("@_durationDate",SqlDbType.Int),
                                     new SqlParameter("@_durationTime",SqlDbType.Int),
                                     new SqlParameter("@_description",SqlDbType.NVarChar,300),
                                     new SqlParameter("@_denomination",SqlDbType.Float),
                                     new SqlParameter("@_discount",SqlDbType.Int),
                                     new SqlParameter("@_total",SqlDbType.Float)};
        parameter[0].Value = _serviceDetail.Id_SD;
        parameter[1].Value = _serviceDetail.PackageName;
        parameter[2].Value = _serviceDetail.DurationDate;
        parameter[3].Value = _serviceDetail.DurationTime;
        parameter[4].Value = _serviceDetail.Description;
        parameter[5].Value = _serviceDetail.Denomination;
        parameter[6].Value = _serviceDetail.Discount;
        parameter[7].Value = _serviceDetail.Total;
        _myConn.ExecuteNonQuery(spName, parameter);
    }
    public void UpdateServiceType(ServiceDto _servicedto)
    {
        string spName = "sp_Update_ServiceType";
        SqlParameter[] parameter = { new SqlParameter("@id_ST", SqlDbType.Int),
                                     new SqlParameter("@_serviceName",SqlDbType.NVarChar,20)};
        parameter[0].Value = _servicedto.Id_ST;
        parameter[1].Value = _servicedto.ServiceName;        
        _myConn.ExecuteNonQuery(spName, parameter);
    }

    public void DeleteServiceDetail(int id_SD)
    {
        string spName = "sp_Delete_ServiceDetail";
        SqlParameter[] parameter = { new SqlParameter("@id_SD",SqlDbType.Int)};
        parameter[0].Value = id_SD;
        _myConn.ExecuteNonQuery(spName,parameter);
    }
    public void InsertServiceDetail(ServiceDetail _serviceDetail)
    {
        string spName = "sp_Insert_ServiceDetail";
        SqlParameter[] parameter = { new SqlParameter("@id_ST", SqlDbType.Int),
                                     new SqlParameter("@_packageName",SqlDbType.NVarChar,20),
                                     new SqlParameter("@_durationDate",SqlDbType.Int),
                                     new SqlParameter("@_durationTime",SqlDbType.Int),
                                     new SqlParameter("@_description",SqlDbType.NVarChar,300),
                                     new SqlParameter("@_denomination",SqlDbType.Float),
                                     new SqlParameter("@_discount",SqlDbType.Int),
                                     new SqlParameter("@_total",SqlDbType.Float),
                                     new SqlParameter("@_status",SqlDbType.Bit)};
        parameter[0].Value=_serviceDetail.Id_ST;
        parameter[1].Value=_serviceDetail.PackageName;
        parameter[2].Value=_serviceDetail.DurationDate;
        parameter[3].Value = _serviceDetail.DurationTime;
        parameter[4].Value = _serviceDetail.Description;
        parameter[5].Value = _serviceDetail.Denomination;
        parameter[6].Value = _serviceDetail.Discount;
        parameter[7].Value = _serviceDetail.Total;
        parameter[8].Value = _serviceDetail.Status;
        _myConn.ExecuteNonQuery(spName, parameter);
    }
    
    public List<ServiceDto> SelectAllServicesType()
    {
        string spName = "sp_Select_AllServicesType";
        DataTable table = _myConn.GetDataTable(spName,1);
        return this.MakeListServices(table);
    }
    public ServiceDto SelectServiceActive(string serviceName)
    {
        string spName = "sp_Select_ServiceActive";
        SqlParameter[] parameter = { new SqlParameter("@serviceName", SqlDbType.NVarChar, 30) };
        parameter[0].Value = serviceName;
        DataTable table = _myConn.GetDataTable(spName, parameter);
        return new ServiceDto(table);
    }
    public ServiceDto SelectService(string serviceName)
    {
        string spName = "sp_Select_Service";
        SqlParameter[] parameter = {new SqlParameter("@serviceName",SqlDbType.NVarChar,30)};
        parameter[0].Value = serviceName;
        DataTable table=_myConn.GetDataTable(spName,parameter);
        return new ServiceDto(table);
    }
    public ServiceDto SelectOtherService()
    {
        string spName = "sp_Select_OtherService";
        DataTable table = _myConn.GetDataTable(spName, 1);
        return new ServiceDto(table);
    }
    public ServiceDto SelectBillAdv(int id_MN, int id_ST, bool billStatus, string orderby, string sort)
    {
        string spName = "sp_Select_BillAdv";
        SqlParameter[] parameter = {   new SqlParameter("@id_MN", SqlDbType.Int),
                                       new SqlParameter("@id_ST",SqlDbType.Int), 
                                       new SqlParameter("@_billStatus",SqlDbType.Bit),
                                       new SqlParameter("@_order",SqlDbType.NVarChar,20),
                                       new SqlParameter("@_desc",SqlDbType.NVarChar,10)};
        parameter[0].Value = id_MN;
        parameter[1].Value = id_ST;
        parameter[2].Value = billStatus;
        parameter[3].Value = orderby;
        parameter[4].Value = sort;
        DataTable table = _myConn.GetDataTable(spName, parameter);
        return new ServiceDto(table);
    }
    public List<ServiceDto> MakeListServices(DataTable table)
    {
        List<ServiceDto> list = new List<ServiceDto>();        
        foreach (DataRow row in table.Rows)
        {            
            list.Add(new ServiceDto(row));
        }
        return list;
    }
}